package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public List<AgreementItemVo> listItem(String phone) {

        return leaseAgreementMapper.listItem(phone);
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        //获取租约信息
        LeaseAgreement leaseAgreement = this.getById(id);
        //todo 单独查每个需求数据
        //获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
        //获取房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        //获取公寓图片信息
        List<GraphInfo> apartmentGraphList = graphInfoMapper.selectGraphByItemIdAndItemType(apartmentInfo.getId(), ItemType.APARTMENT);
        //获取房间图片信息
        List<GraphInfo> roomGraphList = graphInfoMapper.selectGraphByItemIdAndItemType(roomInfo.getId(), ItemType.ROOM);
        //获取支付方式
        List<PaymentType> paymentTypes = paymentTypeMapper.selectPaymentTypeByRoomId(roomInfo.getId());
        //获取租约租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        //装配数据
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());//公寓名称
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphList);//公寓图片列表
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());//房间号
        agreementDetailVo.setRoomGraphVoList(roomGraphList);//房间图片列表
        agreementDetailVo.setPaymentTypeName(paymentTypes.get(0).getName());//支付方式
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());//租期月数
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());//租期单位

        return agreementDetailVo;
    }
}




